import sys
from matplotlib import pyplot as plt
from lib.options import *
from lib.fundInfo import *
from lib.securityInfo import *
from datetime import date,datetime
from common.statistic import *
import scipy.stats as scs
from googlefinance import getQuotes
from common.webHelper import *
from common.portfolio import *
from lib.fundestimation import *
from practice.ch2 import *

if __name__ == '__main__':
    data = BabyNames()
    # result = data[(data.name=='Mary')&(data.sex=='F')]
    # print(result)
    data.get_quantile_index_by_year_sex()
    # mr = MovieLines()
    # print(mr.data[:10])
    # print(mr.get_most_diff_between_sex())
    # gov= USA_GOV()
    # urls = gov.get_url_class()
    # print(urls)
    # print(gov.draw_summarize_win_not_win_users())
    # print(gov.get_tz_counts())
    # gov.get_tz_counts()[:10].plot(kind='barh')
    # plt.show()
    # print(gov.records.ix[10,['tz']])
    # print(gov.records[:10])
    # print(gov.get_most_offen_time_zones())
    # tz = gov.get_tz_counts()
    # print(len(tz))
    # tz2 = gov.get_most_offen_time_zones(10000)
    # print(len(tz2))
    # fs = FundEstimation(2)
    # sd = pd.DataFrame(fs.get_var())
    # sd.plot()
    # plt.show()
    # print(sd)
    # print(sd.shape)
    # np.log(sd.ix[50]/sd.ix[0]).plot()
    # print(sd.ix[0])
    # log_return.plot()
    # plt.show()
    # fs.DailyNetValueDF[['NetValuePerUnit','NetValue']].plot(grid=True)
    # plt.show()
    # x = np.linspace(0, 10, 10)
    # y = np.sin(x)
    # tck = splrep(x, y)
    # x2 = np.linspace(0, 10, 200)
    # y2 = splev(x2, tck)
    # plt.plot(x, y, 'o', x2, y2)
    # plt.show()
    # data = FianceInfo.get_quote_sample('2010-9-1', '2014-9-2')
    # data.to_csv('resources/portfolio.csv')

    # print(np.linspace(0.0,0.25,50))

    # data = pd.read_csv('resources/portfolio.csv', parse_dates=True, index_col=0)
    # porfolio = Portfolio(data,data.columns)
    # porfolio.draw_monto_carlo_chart_with_weights()
    # tvols = porfolio.get_vols_with_effective_returns()
    # opt = porfolio.get_effective_capital_market_line(0.04)

    # StatisticUtility.generte_portfolio_return_vol_chart(data, 2500, trets, tvols)

    # print(opt)



    # StatisticUtility.generte_portfolio_return_vol_chart(data,2500,returns,vols)

    # data = FianceInfo.get_quote_sample('2010-9-1','2014-9-2')
    # data.to_csv('resources/portfolio.csv')
    # print(data)


    # data = pd.read_csv('resources/portfolio.csv',parse_dates=True,index_col=0)
    # print(data)
    # (data/data.ix[0]).plot()


    # StatisticUtility.generte_portfolio_return_vol_chart(data,2500)
    # rets = np.log(data/data.shift(1))
    # StatisticUtility.get_weight_with_maximum_sharp_given_vol(rets,len(data.columns))
    # stats = StatisticUtility.get_portfolio_statistics(rets,weights)
    # print(stats)




    # fig = plt.figure()
    # # fig.add_subplot(2,1)
    # fig, axis = plt.subplots(2, 1, sharex=True, sharey=False)
    # ax1 = axis[0]
    # ax2 = axis[1]
    # ax1.plot(data/data.ix[0])
    # ax2.plot(np.log(data/data.shift(1)))
    # plt.show()


    # data.index =
    #
    # mydata2 = quandl.get("WIKI/MSFT", start_date="2011-10-7", end_date="2011-10-8")
    # df = DataFrame({'AAPL':mydata1['Close'],'GLD':mydata2['Close']})
    # df.index = mydata1.index
    # print(data)
    # myOption = options()
    # fundid = myOption.parse(sys.argv)
    # if(fundid==''):
    #     print("No valid fund id is given!")
    # else:
    #     print('Fund id to parse %s' % fundid)
    # myFund = FundInfo('6155')
    # print 'One year: %.5f' % myFund.getYield(YieldPeriod.OneYear)
    # print 'One month: %.5f' % myFund.getYield(YieldPeriod.OneMonth)
    # print 'Since born: %.5f' % myFund.getYield(YieldPeriod.SinceBorn)

    # fromDTStr = datetime(2014,1,1).strftime('%Y-%m-%d')
    # toDTStr = datetime(2015, 12, 30).strftime('%Y-%m-%d')
    #
    # securityId='600019'
    # security = SecurityInfo(securityId)
    # print(security.getVaR(fromDTStr,toDTStr,0.95))

    # paths = 10000
    # timeSteps = 1
    # priceArray1 = security.getLogReturnsDistribution(fromDTStr,toDTStr)
    # priceArray3 = priceArray1.sort_values(ascending=False)
    # print(priceArray3[-500:-499].round(4))
    # print(priceArray3.quantile(0.05).round(4))

    # securityId = '601669'
    # security = SecurityInfo(securityId)
    # print(security.getVaR(fromDTStr, toDTStr,0.95))
    #
    # priceArray2 = security.getLogReturnsDistribution(fromDTStr, toDTStr)
    #
    # portfolio = DataFrame({'Security1':priceArray1,"Security2":priceArray2})
    # portfolio['composReturn'] = portfolio['Security1']*0.4+portfolio['Security2']*0.6
    # portfolio =  portfolio.sort_values(by=['composReturn'],ascending=False)
    # print (portfolio[-500:-499])
    # print (portfolio[-100:-99])
    #
    # portfolio.hist(bins=50)
    # plt.show()

    # r=0.05
    # sigma=0.2
    # S0=100
    # T=1.0
    # M=50
    # I=250000
    # paths = StatisticUtility.get_paths(S0,r,sigma,T,M,I)
    # logreturns = np.log(paths[1:]/paths[0:-1])
    # print(logreturns)
    # print(logreturns.flatten())
    # StatisticUtility.normality_tests(arr = (logreturns.flatten()))

    # print(paths[0:-1])
    # plt.plot(paths[:,10])
    # plt.grid(True)
    # plt.xlabel('time series')
    # plt.ylabel('index level')
    # plt.show()
    # print(logreturns.shape)
    # print(paths.shape)
    # print(logreturns[:,:50])

    # print(df)
    # del data['AAPL']
    # (data/data.ix[0]*100.).plot(figsize=(8,6))

    # log_returns[['AAPL','SPX']].plot()
    # log_returns.hist(bins=50,figsize=(9,6))
    # symbols = ['AAPL','MSFT','XOM','SPX']
    # for symbol in symbols:
    #     print('%s kurtosis %13.4f ' % (symbol ,  scs.kurtosis(log_returns[symbol].dropna())) )

    # StatisticUtility.generateQQPlot(log_returns['SPX'])
    # plt.show()

    # bnds = tuple((0, 1) for x in range(4))
    # print(bnds)
    #
    # print(getQuotes('AAPL'))

    # data = pandas.read_csv('resources/stock_px.csv', parse_dates=True, index_col=0)
    # data = data['2010-1-2':'2011-1-2']
    # # StatisticUtility.generte_portfolio_return_vol_chart(data,10000)
    # daily_log_returns = np.log(data/data.shift(1))
    # # print(daily_log_returns)
    # weights = np.random.random(4)
    # weights = weights/weights.sum()
    # print(weights)
    # print(StatisticUtility.get_portfolio_statistics(daily_log_returns,weights))
    # periods = len(data.index)
    # log_returns = np.log(data / data.shift(1))
    # # print(log_returns)
    # covv = log_returns.cov()
    #
    # weights = np.random.random(4)
    # weights = weights/weights.sum()
    # # total_exp_return = (log_returns.mean()*weights).sum()
    # # print(total_exp_return * periods)
    #
    # #expected portfolio variance
    # r1= np.dot(weights.T, np.dot(covv * periods,weights))
    # print(np.sqrt(r1) )


